package br.eng.eliseu.gwt.gestaoWeb.server.command.generico;

import java.util.Collection;

import br.eng.eliseu.gwt.gestaoWeb.model.BaseEntity;
import br.eng.eliseu.gwt.gestaoWeb.server.command.BaseCommand;
import br.eng.eliseu.gwt.gestaoWeb.shared.ServiceException;

import com.google.inject.persist.Transactional;

public class DeleteEntitiesCommand extends BaseCommand<Boolean> /*implements Callable<Boolean>*/{
	private static final long serialVersionUID = -8484214588266252805L;

	//	@Inject EntityManager em;
	
	@SuppressWarnings("rawtypes")
	private Class clazz;
	private Collection<Integer> ids;
	
	@Transactional
	public Boolean call() throws ServiceException{
		if(clazz == null) {
			throw new ServiceException("Use o metodo setClazz(...) antes de chamar o call()");
		} else if(ids == null) {
			throw new ServiceException("Use o metodo setIds(...) antes de chamar o call()");
		}
		
		for(Integer id : ids) {
			@SuppressWarnings("unchecked")
			BaseEntity entity = (BaseEntity)getEntityManager().find(clazz, id);
			getEntityManager().remove(entity);
		}
		return true;
	}

	@SuppressWarnings("rawtypes")
	public Class getClazz() {
		return clazz;
	}

	public void setClazz(@SuppressWarnings("rawtypes") Class clazz) {
		this.clazz = clazz;
	}

	public Collection<Integer> getIds() {
		return ids;
	}

	public void setIds(Collection<Integer> ids) {
		this.ids = ids;
	}

}
